package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.entity.Dept;
import com.itheima.pojo.vo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list(){
        log.info("查询所有部门信息");
        return Result.success(deptService.list());
    }

    @Log
    @DeleteMapping
    public Result removeById(Integer id){
        log.info("删除部门，id为{}",id);
        deptService.removeById(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result save(@RequestBody Dept dept){
        log.info("新增部门");
        deptService.saveDept(dept);
        return Result.success();
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("根据id修改部门id:{},name:{}",dept.getId(),dept.getName());
        deptService.update(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("查询部门id为{}",id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }
}
